<?php

namespace App\Traits;

use App\Services\Api\Api;
trait userTrait
{

    public function userLoginTrait($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'phone' => $data['phone'],
            'pwd' => $data['pwd'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        return ($result && $result['succ'] == 1) ? [
            'company_id' => $result['custId'],
            'company_name' => $result['custName'],
            'user_account_string' => $result['custNo'],
            'company_type' => $result['custType'],
            'token' => @$result['token'],
            'account_info' => [
                'user_phone' => $result['accountPhone'],
                'contact_phone' => $result['userPhone'],
                'email' => $result['email'],
                'address' => $result['addr'],
            ],
        ] : false;
    }

    public function userRegistTrait($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'phone' => $data['phone'],
            'pwd' => $data['pwd'],
            'from' => $data['from'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        return ($result && $result['succ'] == 1) ? true : false;
    }

    public function getUserPhoneInfo($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'phone' => $data['phone'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        return $result;
    }

    public function getUserBaseInfoTrait($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'phone' => $data['phone'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        return ($result && $result['succ'] == 1) ? [
            'company_id' => $result['custId'],
            'company_name' => $result['custName'],
            'company_type' => $result['custType'],
            'user_account_string' => $result['custNo'],
            'account_info' => [
                'user_phone' => $data['phone'],
                'contact_phone' => $result['userPhone'],
                'email' => $result['email'],
                'address' => $result['addr'],
            ],
        ] : false;
    }

    public function userForgetPwdTrait($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'phone' => @$data['account_phone'],
            'oldPwd' => @$data['old_pwd'],
            'newPwd' => @$data['new_pwd'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        return ($result && $result['succ'] == 1) ? true : false;
    }

    public function updateUserBaseInfo($apiCode, $data)
    {
        list($province, $city, $area) = explode(',', $data['contact_address_id']);
        $obj = new Api();
        $formData = [
            'phone' => $data['account_phone'],
            'userPhone' => $data['contact_phone'],
            'email' => $data['contact_email'],
            'addr' => [
                'p' => $province,
                'c' => $city,
                'a' => $area,
                'd' => $data['contact_address'],
            ],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        return ($result && $result['succ'] == 1) ? true : false;
    }

    /**
     * 用户认证提交审核
     * @param  [type] $apiCode [description]
     * @param  [type] $data    [description]
     * @return [type]          [description]
     */
    public function authUserSubmitCheck($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'custId' => $data['company_id'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        return ($result && $result['succ'] == 1) ? true : false;
    }

    public function checkUserExitsTrait($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'phone' => $data['phone'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        return ($result && $result['exist'] == 1) ? true : false;
    }

}
